<?php
require_once ('../lib/config.php');
require_once ('../lib/facebook.php');
require_once ('../lib/friends.php');
require_once ('../lib/friend.php');
require_once ('../lib/test.php');
require_once ('f:\wamp\www\simpletest\autorun.php');
require_once ('../lib/init.php');

class TestTemplate extends UnitTestCase {

    function setUp(){
        
        global $login_button;
        
        try{
            $me = getOneself();
        }catch(Exception $e){
            print($login_button);
            exit();
        }
    }
    
    //refer to http://www.simpletest.org/en/unit_test_documentation.html
    function testGetActivities(){ 

        $id = 612139051;
        
        $response = getActivities($id);
        print_r($response);        
        //Result is not Null
        $this->assertNotNull($response);

    }
    
    function testQuery1(){
        $id = 612139051;
        $inMessageQuery = "SELECT thread_id, author_id, body, created_time FROM message WHERE author_id = '$id' AND thread_id IN (
                    	SELECT thread_id FROM thread WHERE (folder_id = 0 OR folder_id = 1) AND updated_time > ".QUERY_SINCE."
                	) AND created_time > ".QUERY_SINCE;
            
        $response = callFQL($inMessageQuery);
        
//        print_r($response);

        $this->assertNotNull($response);
        $this->assertTrue(is_array($response));        
    }
    
    function testQuery11(){
        $id = 612139051;
        $outMessageQuery = "SELECT thread_id, author_id, body, created_time FROM message WHERE author_id = me() AND thread_id IN (
                    	SELECT thread_id FROM thread WHERE (folder_id = 0 OR folder_id = 1) AND updated_time > ".QUERY_SINCE."
                	) AND created_time > ".QUERY_SINCE;
            
        $outMessageQuery2 = "SELECT thread_id, recipients, subject FROM thread WHERE thread_id IN (
                            	SELECT thread_id FROM #outMessageQuery
                            )";
        
        
        
        $response = callMultiFQL(array(
            'outMessageQuery' => $outMessageQuery,
            'outThreadQuery' => $outMessageQuery2
        ));
        
//        print_r($response);

        $this->assertNotNull($response);
        $this->assertTrue(is_array($response));        
    }    
    
    function testQuery2(){
        $id = 612139051;
        
        $friendsCommentQuery = "SELECT fromid, text, time FROM comment
            			WHERE fromid = '$id' 
            			AND (
            				object_id IN (SELECT note_id FROM note WHERE uid = me() AND created_time > ".QUERY_SINCE.")
            				OR object_id IN (SELECT status_id FROM status WHERE uid = me() AND time > ".QUERY_SINCE.")
            				OR object_id IN (SELECT link_id FROM link WHERE owner = me() AND created_time > ".QUERY_SINCE.")
            			) AND time >".QUERY_SINCE;   
                 
        $response = callFQL($friendsCommentQuery);
        
//        print_r($response);

        $this->assertNotNull($response);
        $this->assertTrue(is_array($response));   
    }

    function testQuery3(){
        $id = 612139051;
        
        //My comments on friends' posts
        $myCommentQuery = "SELECT fromid, text, time FROM comment 
                WHERE fromid = me() 
                AND (
    				object_id IN (SELECT note_id FROM note WHERE uid = '$id' AND created_time > ".QUERY_SINCE.")
    				OR object_id IN (SELECT status_id FROM status WHERE uid = '$id' AND time > ".QUERY_SINCE.")
    				OR object_id IN (SELECT link_id FROM link WHERE owner = '$id' AND created_time > ".QUERY_SINCE.")
                ) AND time >".QUERY_SINCE; 
                 
        $response = callFQL($myCommentQuery);
        
//        print_r($response);

        $this->assertNotNull($response);
        $this->assertTrue(is_array($response));   
    }
    
    function testQuery4(){
        $id = 612139051;
        
        $friendsLikesQuery = "SELECT object_id FROM like 
            			WHERE user_id = '$id' 
            			AND (
            				object_id IN (SELECT note_id FROM note WHERE uid = me() AND created_time > ".QUERY_SINCE.")
            				OR object_id IN (SELECT status_id FROM status WHERE uid = me() AND time > ".QUERY_SINCE.")
            				OR object_id IN (SELECT link_id FROM link WHERE owner = me() AND created_time > ".QUERY_SINCE.")
            			)";
                 
        $response = callFQL($friendsLikesQuery);
        
//        print_r($response);

        $this->assertNotNull($response);
        $this->assertTrue(is_array($response));   
    }
    
    function testQuery5(){
        $id = 612139051;
        
        $myLikeQuery = "SELECT object_id FROM like 
                        WHERE user_id = me() 
                        AND (
            				object_id IN (SELECT note_id FROM note WHERE uid = '$id' AND created_time > ".QUERY_SINCE.")
            				OR object_id IN (SELECT status_id FROM status WHERE uid = '$id' AND time > ".QUERY_SINCE.")
            				OR object_id IN (SELECT link_id FROM link WHERE owner = '$id' AND created_time > ".QUERY_SINCE.")
                        )";
                 
        $response = callFQL($myLikeQuery);
        
//        print_r($response);

        $this->assertNotNull($response);
        $this->assertTrue(is_array($response));   
    }
    
    function testQuery6(){
        $id = 612139051;
        
        $friendPostQuery = "SELECT post_id, actor_id, message, created_time FROM stream 
                        WHERE source_id = me() AND actor_id = '$id' AND created_time >".QUERY_SINCE;
                 
        $response = callFQL($friendPostQuery);
        
//        print_r($response);

        $this->assertNotNull($response);
        $this->assertTrue(is_array($response)); 
    }
    
    function testQuery7(){
        $id = 100000526102422;
        
        //$myPostQuery = "select filter_key, name from stream_filter where uid = me()";
        $myPostQuery = "SELECT post_id, actor_id, message, created_time FROM stream 
                        WHERE source_id = '$id' AND target_id = '$id' AND actor_id = me() AND created_time >".QUERY_SINCE;
                 
        $response = callFQL($myPostQuery);
        
//        print_r($response);

        $this->assertNotNull($response);
//        $this->assertTrue(is_array($response)); 
    }
}
?>